# -*- coding: utf-8 -*-
import scrapy
from znfls.items import ZnflsItem

class ZnflsSpider(scrapy.Spider):
    name = "Znfls"    
    download_delay = 2 
    #allowed_domains = ["www.zhainanfulishe.net"]
    start_urls = ['http://www.zhainanfulishe.net//nvshen']

    def parse(self, response):
    	self.log('A response from %s just arrived!' % response.url)
    	lst = response.xpath('//div[@class="home-blog-entry-text clr"]/h3/a')
    	for a in lst:
    		item = ZnflsItem()
    		item['title'] = a.xpath('@title').extract()[0]
    		item['link'] = a.xpath('@href').extract()[0]
    		#print a.xpath('@href').extract()[0], a.xpath('@title').extract()[0]
    		yield scrapy.Request(item['link'], meta = {'item': item}, callback = self.parse_contents)        	

        nextpage = response.xpath('//span[@class="pg-item pg-nav-item pg-next"]/a/@href').extract()
        if nextpage:
        	nextpage = nextpage[0]
        	print '\n\n\nTHE NEXT LINK', nextpage
        	yield scrapy.Request(nextpage, callback = self.parse)

    def parse_contents(self, response):
    	item = response.meta['item']
    	imgsrcs = []
    	contents = response.xpath('//img[@class="alignnone"]/@src').extract()
    	for imgsrc in contents:
    		imgsrcs.append(imgsrc)
    	item['imglinks'] = imgsrcs
    	yield item